<?php
namespace app\client\controller;

class Reward {
    public function __construct() {

    }
    
    public function invite() {
        $params = request()->post();
        $userId = isset($params['key']) ? trim($params['key']) : '';
        if($userId === '') {
            return errorResult('rtr');
        }
        
        $type = isset($params['type']) ? intval($params['type']) : 1; //1拉新，2：邀请商家,2:邀请代理商
        if(!in_array($type, [1, 2, 3])) {
            return errorResult('rtre');
        }
        
        
        $userId = strrev($userId);
        $userId = hexdec($userId) / 23;
        
        $user = model('User')->findOne(['user_id' => $userId], ['inviter']);
        if(empty($user) || $user['inviter'] == 0) {
            return errorResult('aa');
        }
        if($type == 1) {
            $count = model('Chat')->where(['user_id' => $userId])->count();
            if($count != 1) {
                return errorResult('bb');
            }
        }
        
        $inviter = $user['inviter'];
        $where = ['user_id' => $inviter, 'hit_user_id' => $userId, 'type' => $type];
        if($type == 3) {
            $target = isset($params['target']) ? intval($params['target']) : 0;
            if($target <= 0) {
                return errorResult('kk');
            }
            
            $agent = model('ApplyAgentOrder')->findOne(['id' => $target], ['amount', 'merchant_id']);
            if(empty($agent)) {
                return errorResult('kk1');
            }
            
            $where['merchant_id'] = $agent['merchant_id'];
        }
        
        $inviteModel = model('InviteLog');
        $res = $inviteModel->findOne($where, ['id']);
        if(!empty($res)) {
            return errorResult('cc');
        }
        
        if($type == 1) {
            $amount = 1;
        }elseif ($type == 2) {
            $amount = 198;
        }else{
            $amount = $agent['amount'];
            $amount *= 0.1;
            $amount = roundNumber($amount, 2);
            if($amount <= 0) {
                return errorResult('zz');
            }
        }
        $time = time();
        
        $inviteModel->startTrans();
        
        try {
            $walltmodel = model('UserWallet');
            $wallet = $walltmodel->where(['user_id' => $inviter])->field(['invite_amount', 'amount'])->lock(true)->find();
            if(empty($wallet)) {
                $inviteModel->rollback();
                return errorResult('dd');
            }
            
            $rs = $walltmodel->modify(['user_id' => $inviter], ['invite_amount' => $wallet['invite_amount'] + $amount, 'amount' => $wallet['amount'] + $amount]);
            if(!$rs) {
                $inviteModel->rollback();
                return errorResult('ee');
            }
            
            $data = ['user_id' => $inviter, 'hit_user_id' => $userId, 'add_time' => $time, 'type' => $type, 'amount' => $amount];
            if($type == 3) {
                $data['merchant_id'] = $agent['merchant_id'];
            }
            $id = $inviteModel->add($data);
            if(!$id) {
                $inviteModel->rollback();
                return errorResult('hh');
            }
            
            $rs = model('UserFinanceLog')->add(['user_id' => $inviter, 'before_amount' => $wallet['amount'], 'amount' => $amount, 'type' => 4, 'related_id' => $id, 'add_time' => $time]);
            if(!$rs) {
                $inviteModel->rollback();
                return errorResult('vv');
            }
            
            $inviteModel->commit();
            
            return successResult();
            
        } catch (\Exception $e) {
            $inviteModel->rollback();
            return errorResult($e->getMessage());
        }
        
    }
    
}